package com.leetcode.math;

public class RotateRight {

    public static void main(String[] args) {
//        RotateRight rotateRight = new RotateRight();
//        rotateRight
    }

    public ListNode rotateRight(ListNode head, int k) {
        ListNode nextNode;
        if (k == 0 || head == null || head.next == null) {
            return head;
        }
        int size = 1;
        ListNode tempNode = head;
        while (tempNode.next != null) {
            tempNode = tempNode.next;
            size++;
        }
        int add = size - k % size;
        if (add == size) {
            return head;
        }
        tempNode.next = head;
        while (add-- > 0) {
            tempNode = tempNode.next;
        }
        ListNode res = tempNode.next;
        tempNode.next = null;
        return res;
    }
}
